package com.cloud.user_info.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.entity.SysDeptArea;
import com.cloud.user_info.mapper.SysDeptAreaMapper;
import com.cloud.user_info.service.ISysDeptAreaService;
import com.cloud.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门地区负责人
 *
 * @author yzj
 * @since 2022-11-21
 */
@Service
public class SysDeptAreaServiceImpl extends ServiceImpl<SysDeptAreaMapper, SysDeptArea> implements ISysDeptAreaService {

    @Resource
    private SysDeptAreaMapper sysDeptAreaMapper;

    /**
     * 部门地区负责人分页
     *
     * @author yzj
     */
    @Override
    public List<SysDeptArea> selectSysDeptAreaList(SysDeptArea deptArea) {
        return sysDeptAreaMapper.selectSysDeptAreaList(deptArea);
    }

    /**
     * 根据主键查询部门负责人
     *
     * @param deptAreaId
     * @return
     */
    @Override
    public SysDeptArea selectDeptAreaInfo(Long deptAreaId) {
        return sysDeptAreaMapper.selectDeptAreaInfo(deptAreaId);
    }

    /**
     * 根据区域编号查询部门负责人
     *
     * @param areaCode
     * @return
     */
    @Override
    public SysDeptArea selectDeptAreaByCode(String areaCode) {
        return sysDeptAreaMapper.selectDeptAreaByCode(areaCode);
    }

    /**
     * 根据主键查询区域信息
     *
     * @param areaId
     * @return
     */
    @Override
    public SysDeptArea selectAreaById(Long areaId) {
        return sysDeptAreaMapper.selectAreaById(areaId);
    }

    /**
     * 根据区域名称查询区域信息
     *
     * @param areaName
     * @return
     */
    @Override
    public SysDeptArea selectAreaByName(String areaName) {
        return sysDeptAreaMapper.selectAreaByName(areaName);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param deptArea 区域信息
     * @return
     */
    @Override
    public List<SysDeptArea> buildAreaTree(SysDeptArea deptArea) {
        List<SysDeptArea> deptAreaList = selectSysDeptAreaList(deptArea);
        if (deptAreaList.size() <= 0) {
            return null;
        }
        List<SysDeptArea> returnList = new ArrayList<>();
        List<Long> tempList = new ArrayList<>();
        for (SysDeptArea deptAreaInfo : deptAreaList) {
            tempList.add(deptAreaInfo.getDeptId());
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(deptAreaInfo.getParentId())) {
                recursionFn(deptAreaList, deptAreaInfo);
                returnList.add(deptAreaInfo);
            }
        }
        if (returnList.isEmpty()) {
            returnList = deptAreaList;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysDeptArea> deptAreaList, SysDeptArea deptArea) {
        // 得到子节点列表
        List<SysDeptArea> childList = getChildList(deptAreaList, deptArea);
        deptArea.setChildren(childList);
        for (SysDeptArea tChild : childList) {
            if (hasChild(deptAreaList, tChild)) {
                recursionFn(deptAreaList, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysDeptArea> getChildList(List<SysDeptArea> deptAreaList, SysDeptArea deptArea) {
        List<SysDeptArea> tlist = new ArrayList<>();
        for (SysDeptArea dept : deptAreaList) {
            if (StringUtils.isNotNull(dept.getParentId())
                    && dept.getParentId().longValue() == deptArea.getDeptId().longValue()) {
                tlist.add(dept);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysDeptArea> deptAreaList, SysDeptArea deptArea) {
        return getChildList(deptAreaList, deptArea).size() > 0;
    }

}
